﻿using Common.Dtos;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;
using Prism.Services.Dialogs;
using Sigma.Admin.Client.DialogAware;
using Sigma.Admin.Client.Extensions;
using Sigma.Admin.Client.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sigma.Admin.Client.ViewModels
{
    [RegionMemberLifetime(KeepAlive = false)]
    public class MenuViewModel : NavigationViewModel
    {
        private readonly IMenuService _menuService;

        public DelegateCommand<string> ExecuteCommand { get; private set; }
        public DelegateCommand<string> DeleteCommand { get; private set; }

        private readonly IDialogHostService _dialog;
        private readonly IRegionManager _regionManager;
        //private readonly SubscriptionToken _token;
        public MenuViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider containerProvider, IMenuService menuService, IDialogHostService dialog) : base(ea, containerProvider)
        {
            //_userService = service;
            ExecuteCommand = new DelegateCommand<string>(Execute);
            DeleteCommand = new DelegateCommand<string>(DeleteMenu);
            _dialog = dialog;
            //_token = _ea.ResgiterMessage(EditOrCancel, "UserVM");
            _menuService = menuService;
            _regionManager = regionManager;
        }
        private ObservableCollection<MenuDto> _menus;

        public ObservableCollection<MenuDto> Menus
        {
            get { return _menus; }
            set { _menus = value; RaisePropertyChanged(); }
        }

        private List<MenuDto> _parentMenus;

        public List<MenuDto> ParentMenus
        {
            get { return _parentMenus; }
            set { _parentMenus = value; RaisePropertyChanged(); }
        }
        async Task GetDataAsync()
        {
            UpdateLoading(true);

            var res = await _menuService.GetMenu();
            
            if (res.Status )
            {
                ParentMenus = res.Result.Where(x => x.Level == 0).ToList();//筛选1级菜单作为父级菜单选择下拉框的datasource
                res.Result.Where(x=>x.Level == 1).ToList().ForEach(x =>
                {
                    x.ParentName = ParentMenus.Where(y => y.Id == x.PId).First().TitleName;
                });
                Menus = new ObservableCollection<MenuDto>(res.Result);
                
            }
            else
            {
                _ea.SendMessage(res.Message);
            }
            UpdateLoading(false);
        }

        private void Execute(string obj)
        {
            switch (obj)
            {
                case "AddMenu":
                    AddMenu(); break;

            }
        }

        public async Task AddMenu()
        {
            DialogParameters param = new DialogParameters();
            param.Add("value", ParentMenus);
            var dialogResult = await _dialog.ShowDialog("AddMenuView", param);
            //添加角色后刷新
            if (dialogResult.Result == ButtonResult.OK)
                await GetDataAsync();
        }

        private async void DeleteMenu(string id)
        {
            if (int.TryParse(id, out var result))
            {
                var res = await _menuService.DeleteMenu(result);
                if (res.Status)
                    await GetDataAsync();
                else if (res.Message is not null)
                    _ea.SendMessage(res.Message);
            }
        }

        public override async void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);
            if (navigationContext.Parameters.GetValue<bool>("IsNewInstance"))//创建新实例才刷新
                await GetDataAsync();
        }
    }
}
